var salt = sessionStorage.getItem('usersalt');
var idrisk = getQueryVariable('id');
var arr = new Array(); //图片id
var arr1 = new Array(); //文件id
var arr2 = new Array();
var danweim = '';
var dutycompay;
layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		table = layui.table;

	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);

	}

	laydate.render({
		elem: '#practic',
		trigger: 'click',
		max: 0
	});

	//*获取所有企业*/
	//     riskcompay();
	//     function riskcompay(){
	//     		$.ajax({
	//			url: url+'baseEnt/getAllEnt',
	//	      		dataType: 'json',
	//	      		async: false,
	//	      		type: "POST",
	//	      		data:{
	//	      			Token:salt
	//	      		},
	//	      		success: function(data) {
	//	      			$('#dutycompay').empty();
	//	      			console.log(data);
	//	      			if(data.code==0){
	//	      				var list=data.data;
	//	      				var opt='<option value="0">请选择</option>'
	//	      				for(var i=0;i<list.length;i++){
	//	      					opt+='<option value="'+list[i].id+'" >'+list[i].name+'</option>'
	//	      				}
	//	      				$('#dutycompay1').append(opt);
	//	      				form.render();
	//	      			}
	//	
	//	      		},
	//	      		error: function(error) {
	//	      			console.log("获取失败");
	//	      		}
	//	      	});
	//     }

	$('#resclas').click(function() {
		$('#resclass').show();
		$('#resclas').val('')
		$.fn.zTree.init($("#resclass"), setting, zNodes);
	});

	var company;

	getallent();

	function getallent() {

		$.ajax({
			url: url + 'baseEnt/getAllEnt',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {

			},
			success: function(data) {
				$('#resclass').empty();
				if(data.code == 0) {
					var list = data.data;
					company = list;

				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});

	}

	var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		check: {
			enable: true,
			chkStyle: "radio", //单选框
			radioType: "all" //对所有节点设置单选
		},
		callback: {
			onCheck: zTreeOnCheck
		}

	};

	var zNodes = company;

	$.fn.zTree.init($("#resclass"), setting, zNodes);

	$('#getval').click(function() {
		getval();
	})

	function zTreeOnCheck(event, treeId, treeNode) {
		var checked = treeNode.checked;
		danweim = treeNode ? treeNode.name : "root"
		$('#resclas').val(danweim)
		$('#resclass').hide();

		getval()
	};

	function getval() {
		arr2 = [];
		danweim = '';
		var treeObj = $.fn.zTree.getZTreeObj("resclass");
		var nodes = treeObj.getCheckedNodes(true),

			v = "";
		for(var i = 0; i < nodes.length; i++) {
			v += nodes[i].name + ",";

			arr2.push(nodes[i].id); //获取选中节点的值
		}
		return arr2;
	}

	/*通过隐患id获取隐患消息*/
	findinfo();

	function findinfo() {
		$.ajax({
			url: url + 'perilInfo/getInfo',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
				Token: salt,
				id: idrisk
			},
			success: function(data) {
				if(data.code == 0) {
					var list = data.data.perilInfo;
					$('#finduser').text(list.findUser); //发现人
					$('#findtime').text(list.findTime); //发现时间
					$('#typeId').text(list.typeName); //隐患类型
					$('#describe').text(list.describe); //隐患描述

					for(var i = 0; i < list.attachments.length; i++) {

						$('#descrimg').append('<div style="display:inline-block;position:relative;margin:10px;" ><img id="pic' + list.attachments[i].id + '" onclick="lookbigpic(\'' + list.attachments[i].id + '\')" src="' + url + 'attachment/seekFile/' + list.attachments[i].id + '" width="100px" height="100px" class="picsty"/></div>')
					}

					$('#cheuser').text(list.auditUser); //核查人
					$('#chetime').text(list.auditTime); //核查时间

					$('#comptime').text(list.finishTime); //防控措施完成时间

					$('#cheaudi').text(list.auditFruit); //核查结论
					$('#auditStep').text(list.auditStep); //防控措施
					$('#leaderUser1').val(list.leaderUser); //责任人
					$('#practic').val(list.endTime);
					//	      				$('#dutycompay1').find('option[value="'+list.leaderUnits+'"]').attr('selected','selected');//责任单位
					$('#resclas').val(list.leaderUnitsName); //责任单位
					arr2 = list.leaderUnits

					var list1 = data.data.perilBills;
					if(list1.length > 0) {
						for(var i = 0; i < list1.length; i++) {
							var flg = '';
							var goverfile = '';
							var schemepic = '';
							var result = '';
							/*获取文件*/
							if(list1[i].result == 0) {
								result = '<a style="color:#1c85d4">已销项</a>';
							} else if(list1[i].result == 1) {
								result = '未销项';
							} else {
								result = '';
							}

							//		      					$('#dutycompay').text(list1[i].leaderUnitsName);//责任单位
							//		      					$('#lushitime').text(list1[i].entTime)
							for(var l = 0; l < list1[i].attachmentsFile.length; l++) {

								goverfile += '<a  style="color:#1c85d4; text-decoration:underline;" href="' + url + 'attachment/seekFile/' + list1[i].attachmentsFile[l].id + '">' + list1[i].attachmentsFile[l].originaName + '</a>&nbsp;&nbsp;';
							}
							for(var h = 0; h < list1[i].attachmentsImg.length; h++) {
								//			      						schemepic+='<img style="margin-right:20px;" src="'+url+'attachment/seekFile/'+list1[i].attachmentsImg[h].id+'" width="100px" />';
								schemepic += '<div style="display:inline-block;position:relative;margin:10px;" ><img id="pic' + list1[i].attachmentsImg[h].id + '" onclick="lookbigpic(\'' + list1[i].attachmentsImg[h].id + '\')" src="' + url + 'attachment/seekFile/' + list1[i].attachmentsImg[h].id + '" width="100px" height="100px" class="picsty"/></div>';
							}
							flg = '<tr><tr><td style="padding:0px;" colspan="6"><hr class="layui-bg-black"></td></tr>' +
								'<tr><td><label class="fowei">责任人</label></td><td><span  id="leaderUser" >' + list1[i].leaderUser + '</span></td><td><label class="fowei">责任单位</label></td>' +
								'<td><span id="dutycompay">' + list1[i].leaderUnitsName + '</span></td><td><label class="fowei">治理措施<br />落实时间</label></td>' +
								'<td><span id="lushitime" class="line-H40">' + list1[i].entTime + '</span></td></tr>' +
								'<tr><td><label class="fowei">治理方案</label></td>' +
								'<td colspan="5"><span style="display: -webkit-box;word-break: break-all;-webkit-box-orient: vertical;overflow: hidden;text-overflow:ellipsis;" >' + list1[i].program + '</span></td>'

								+
								'</tr><tr><td></td><td colspan="5"><div id="goverfile">' + goverfile + '</div></td></tr>' +
								'<tr><td><label class="fowei">治理后照片</label></td><td colspan="5"><div id="schemepic">' + schemepic + '</div></td></tr>';
							if(list1[i].type == 1) {
								flg = '<tr><tr><td style="padding:0px;" colspan="6"><hr class="layui-bg-black"></td></tr>' +
									'<tr><td><div class="layui-inline"><label class="fowei"><label class="fowei">销项核查人</label> </label></td><td>' +
									'<span >' + list1[i].leaderUser + '</span>' +
									'</div></td><td><div class="layui-inline"><label class="fowei">核查人单位</label></td><td>' +
									'<span >' + list1[i].leaderUnitsName + '</span>' +
									'</div></td><td><div class="layui-inline"><label class="fowei">治理措施<br />落实时间 </label></td><td>' +
									'<span >' + list1[i].entTime + '</span></div></td></tr><tr><td><label class="fowei">销项结论</label></td><td colspan="5"><span>' + result + '</span><td></tr>';
							}

							$('#newsdata').append(flg);
						}
					}

					form.render();

				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}

	/*增加治理方案*/
	$('#upsubmit').on('click', function() {

		addgovernparti();
	});

	function addgovernparti() {
		var leaderUser = $.trim($('#leaderUser1').val()); //负责人
		//     		var dutycompay=$('#dutycompay1').val();//责任单位

		dutycompay = arr2.toString(); //责任单位

		var practic = $('#practic').val(); //落实时间
		var treatment = $.trim($('#treatment').val()); //治理方案
		if(leaderUser != '' && dutycompay != 0 && practic.length != '' && treatment.length != '') {
			$.ajax({
				url: url + 'perilInfo/addBill',
				dataType: 'json',
				async: false,
				type: "POST",
				data: {
					Token: salt,
					id: idrisk,
					leaderUser: leaderUser, //负责任人、核查人
					leaderUnits: dutycompay, //负责单位、核查单位
					entTime: practic, //落实时间、核查时间
					program: treatment, //方案
					type: '0',
					attachmentsImg: arr.toString(), //图片id
					attachmentsFile: arr1.toString() //附件id
				},
				success: function(data) {
					if(data.code == 0) {
						layer.confirm('治理方案提交成功！', {
							btn: ['确定'] //按钮
						}, function() {
							location.href = "page/risk/risk-list/risk-list.html";
							layer.closeAll();
						}, function() {

						});
					}

				},
				error: function(error) {
					console.log("获取失败");
				}
			});
		} else {
			layer.msg('请将信息填写完整');
		}

	}

	var flg1 = 0;
	/*上传附件*/

	layui.use('upload', function() {
		var $ = layui.jquery,
			upload = layui.upload;

		upload.render({
			elem: '#addfile',
			url: url + 'attachment/upload',
			multiple: true,
			accept: 'file',
			before: function(obj) {},
			done: function(res) {
				//上传完毕
				if(res.code == 0) {
					flg1++;
					arr1.push(res.data.id);

					var img = $('<div style="display:inline-block;position:relative;margin-right:45px;" id="picupfile' + flg1 + '"  ><div onclick="delfile(\'picupfile' + flg1 + '\',\'' + flg1 + '\',\'' + res.data.id + '\')" style="position:absolute;top:-2px;right:-21px;color:red;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><a style="color: blue;text-decoration: underline;" href="' + url + 'attachment/seekFile/' + res.data.id + '" >' + res.data.originaName + '</a></div>');
					$('#picapp1').append(img);

				}
			}
		});

	});

	var flg = 0;

	/*上传照片*/

	layui.use('upload', function() {
		var $ = layui.jquery,
			upload = layui.upload;

		upload.render({
			elem: '#addpic',
			url: url + 'attachment/upload',
			multiple: true,
			accept: 'file',
			exts: 'png|jpg|gif|jpeg|PNG|JPG|GIF|JPEG',
			before: function(obj) {},
			done: function(res) {
				//上传完毕
				if(res.code == 0) {
					flg++;
					arr.push(res.data.id);

					var img = $('<div style="display:inline-block;position:relative;margin:10px;" id="picupdata' + flg + '"  ><div onclick="delpic(\'picupdata' + flg + '\',\'' + flg + '\',\'' + res.data.id + '\')" style="position:absolute;top:-4px;right:-5px;color:white;background:red;border:1px solid red;border-radius:50%;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><img id="pic' + res.data.id + '" onclick="lookbigpic(\'' + res.data.id + '\')" src="' + url + 'attachment/seekFile/' + res.data.id + '" width="100px" height="100px" class="picsty"/></div>');
					$('#picapp').append(img);

				}
			}
		});

	});

});

/*删除事件*/
function delpic(self, num, inde) {
	layer.confirm('你确定要删除吗？', {
		btn: ['确定', '取消'] //按钮
	}, function() {
		$('#' + self).remove();
		for(var i = 0; i < arr.length; i++) {
			if(arr[i] == inde) {
				arr.splice(i, 1);
			}
		}

		layer.msg('删除成功');
	}, function() {

	});
}

/*删除事件*/
function delfile(self, num, inde) {
	layer.confirm('你确定要删除吗？', {
		btn: ['确定', '取消'] //按钮
	}, function() {
		$('#' + self).remove();
		for(var i = 0; i < arr1.length; i++) {
			if(arr1[i] == inde) {
				arr1.splice(i, 1);
			}
		}

		layer.msg('删除成功');
	}, function() {

	});
}

function lookbigpic(self) {

	var json = {
		"status": 1,
		"msg": "",
		"title": "",
		"id": 8,
		"start": 0,
		"data": [{
			"alt": "",
			"pid": 109,
			"src": url + "attachment/seekFile/" + self,
			"thumb": ""
		}]
	}
	layer.photos({
		photos: json,
		anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
	});

}